import Vue from "vue";
import VueRouter from "vue-router";

import { tokenKey } from "./../utils/constants"
import routes from "./routes"

Vue.use(VueRouter);

const whiteList=["/login","/register","/demo"];

let router=new VueRouter({
	routes,
	mode: 'hash',      			    //默认hash,可以使用history
	scrollBehavior: (t,f,s) => ({   //因为是单页面应用，默认让滚动条回到0 0 的位置，
		x:0,
		y: 0
	})
});

router.beforeEach((to, from, next) => {
	var token=window.localStorage.getItem(tokenKey);
	if(token){
		next();
	}else{		
		if(whiteList.indexOf(to.path)!="-1"){
			next();
		}else{
			next("/login")
		}
	}
});

export default router;